from redis import Redis, ConnectionPool

class Mredis():
    def __init__(self):
        self.pool = ConnectionPool(host='localhost', port=6379, db=2)
        self.r = Redis(connection_pool=self.pool)

    def rpush(self, key, value):
        return self.r.lpush(key, value)

    def lpop(self, key):
        return self.r.lpop(key)

    def lrange(self, key, start, end):
        return self.r.lrange(key, start, end)

    def llen(self, key):
        return self.r.llen(key)

    def rpoplpush(self, key1, key2):
        return self.r.rpoplpush(key1, key2)

    def lrem(self, key, count, value):
        """从列表中删除元素
        :param key: Redis列表的key
        :param count: 删除的数量，0表示删除所有匹配项
        :param value: 要删除的值
        """
        return self.r.lrem(key, count, value)
    def delete(self, key):
        return self.r.delete(key)
    
    def set_str(self, key, value):
        """设置字符串类型的键值对"""
        return self.r.set(key, value)
    
    def get_str(self, key):
        """获取字符串类型的值"""
        value = self.r.get(key)
        if value:
            return value.decode('utf-8')
        return None
    
    def expire(self, key, seconds):
        """设置键的过期时间（秒）"""
        return self.r.expire(key, seconds)

r = Mredis()